package logic

import (
	"context"
	"errors"
	"video-up-manager/models/mysql"

	"video-up-manager/videorpc/internal/svc"
	"video-up-manager/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectionLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectionLogic {
	return &CollectionLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectionLogic) Collection(in *videorpc.CollectionRequest) (*videorpc.CollectionResponse, error) {
	// todo: add your logic here and delete this line

	var collection mysql.Collection
	err := collection.FindCollectionById(mysql.DB, in.UserId, in.VideoId)
	if err != nil {
		return nil, errors.New("已收藏该视频")
	}

	collection = mysql.Collection{
		UserId:        in.UserId,
		VideoId:       in.VideoId,
		CollectTime:   in.CollectTime,
		CollectStatus: in.CollectStatus,
	}

	if err := collection.CollectionAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &videorpc.CollectionResponse{
		CollectionId: int64(collection.ID),
	}, nil
}
